package com.zxy.supplier_system.server.controller

import com.zxy.supplier_system.server.entity.Authority
import com.zxy.supplier_system.server.entity.Goods
import com.zxy.supplier_system.server.entity.GoodsUnit
import com.zxy.supplier_system.server.repository.QueryGoodsUnitResponse
import com.zxy.supplier_system.server.repository.toQueryGoodsUnitResponse
import com.zxy.supplier_system.server.service.MarketDeliveryOrderService
import com.zxy.supplier_system.server.utils.LongIdAndName
import com.zxy.supplier_system.server.utils.toLongIdAndName
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("markets/deliveryOrders/{orderId}/items")
class MarketDeliveryOrdersItemController(
    private val marketDeliveryOrderService: MarketDeliveryOrderService
) {

    data class AddMarketDeliveryOrdersItemRequest(
        val goodsId: Int,
        val amount: Int,
        val unitId: Long?,
        val subGoodsId:Int?,
    )

    @HasAnyAuthority(Authority.卖场_送货单_修改, Authority.卖场_送货单_验收, Authority.卖场_送货单_审核)
    @PostMapping
    fun addItem(
        @PathVariable orderId: Int,
        @RequestBody addMarketDeliveryOrdersItemRequest: AddMarketDeliveryOrdersItemRequest
    ) {
        this.marketDeliveryOrderService.addItem(orderId, addMarketDeliveryOrdersItemRequest)
    }

    @HasAnyAuthority(Authority.卖场_送货单_修改, Authority.卖场_送货单_验收, Authority.卖场_送货单_审核)
    @DeleteMapping("{goodsId}")
    fun deleteItem(@PathVariable goodsId: Int, @PathVariable orderId: Int) {
        this.marketDeliveryOrderService.deleteItem(orderId, goodsId)
    }

    @Transactional
    class UpdateItemAmountRequest(
        val amount: Int,
        val unitId: Long?,
    )

    @HasAnyAuthority(Authority.卖场_送货单_修改, Authority.卖场_送货单_验收, Authority.卖场_送货单_审核)
    @PatchMapping("{goodsId}/amount")
    fun updateItemAmount(
        @PathVariable goodsId: Int,
        @PathVariable orderId: Int,
        @RequestBody updateItemAmountRequest: UpdateItemAmountRequest
    ) {
        return this.marketDeliveryOrderService.updateItemAmount(goodsId, orderId, updateItemAmountRequest)
    }

    @HasAnyAuthority(Authority.卖场_送货单_查询)
    @GetMapping
    fun queryOrderItemsInOrder(@PathVariable orderId: Int, pageable: Pageable): Page<OrderItemInOrderSummary> {
        return this.marketDeliveryOrderService.queryOrderItemsInOrder(orderId, pageable)
    }

    @HasAnyAuthority(Authority.卖场_送货单_修改, Authority.卖场_送货单_验收, Authority.卖场_送货单_审核)
    @GetMapping("{goodsId}/exists")
    fun exists(@PathVariable goodsId: Int, @PathVariable orderId: Int): Boolean {
        return this.marketDeliveryOrderService.exists(orderId, goodsId)
    }

    @HasAnyAuthority(Authority.卖场_送货单_修改, Authority.卖场_送货单_验收, Authority.卖场_送货单_审核)
    @GetMapping("barcodes/{barcode}/exists")
    fun exists(@PathVariable barcode: String, @PathVariable orderId: Int): Boolean {
        return this.marketDeliveryOrderService.exists(orderId, barcode)
    }

    data class OrderItemInOrderSummary(
        val goodsId: Int,
        val name: String,
        val barcode: String,
        val amountInOrder: Int,
        val inStore: Int,
        val boxSpecification: String?,
        val units: List<QueryGoodsUnitResponse>,
        val unit: LongIdAndName?,
    ) {
        companion object {
            fun fromGoodsAndAmountAndUnit(goods: Goods, amount: Int, goodsUnit: GoodsUnit?): OrderItemInOrderSummary {

                return OrderItemInOrderSummary(
                    goods.id,
                    goods.name,
                    goods.barcode,
                    amount,
                    goods.amount,
                    goods.boxSpecification,
                    goods.units.map { it.toQueryGoodsUnitResponse() },
                    goodsUnit?.toLongIdAndName(),
                )
            }
        }
    }

    @HasAnyAuthority(Authority.卖场_送货单_修改, Authority.卖场_送货单_验收, Authority.卖场_送货单_审核)
    @PostMapping("barcodes/{barcode}")
    fun addItem(@PathVariable barcode: String, @PathVariable orderId: Int): OrderItemInOrderSummary {
        return this.marketDeliveryOrderService.addItem(orderId, barcode)
    }

    @HasAnyAuthority(Authority.卖场_送货单_修改, Authority.卖场_送货单_验收, Authority.卖场_送货单_审核)
    @PostMapping("goods/{goodsId}")
    fun addItem(@PathVariable goodsId: Int, @PathVariable orderId: Int): OrderItemInOrderSummary {
        return this.marketDeliveryOrderService.addItem(orderId, goodsId)
    }

    @DeleteMapping
    fun clear(@PathVariable orderId: Int){
        return this.marketDeliveryOrderService.clear(orderId)
    }

}